package handlers

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"gin_study/app/event_manager"
	"gin_study/app/events"
	"gin_study/app/services"
)

func ShowHello(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"name": "wangzhihui",
	})
}

func ShowUsers(c *gin.Context) {
	users := services.FetchUserLists()
	c.JSON(http.StatusOK, users)
}

func ChangeUserName(c *gin.Context) {
	id := c.DefaultQuery("id", "")
	name := c.DefaultQuery("name", "")
	update := services.ChangeUserName(id, name)
	c.JSON(http.StatusOK, update)
}

func FindOneUser(c *gin.Context) {
	id := c.DefaultQuery("id", "")
	user := services.FindOneUser(id)
	e := events.FetchUserEvent{Name: "HAHAHAHAH"}
	event_manager.EM.Dispatch(e)
	c.JSON(http.StatusOK, user)
}
